using Engine;
using Game;

namespace SCIENEW {
    public enum Materials
    {
        None = 0,
        Steel,
        Gold,
        Silver,
        Lead,
        Platinum,
        Zinc,
        Stannary,
        Chromium,
        Titanium,
        Nickel,
        Aluminum,
        Uranium,
        Phosphorus,
        Iron,
        Copper,
        Mercury,
        Germanium,
        FeAlCrAlloy,
        Brass,
        Plastic,
        Vanadium,
        Cupronickel,
        //Gun_Steel,
    }

    public static class MaterialsUtils {

        public static readonly Dictionary<Materials, int> materialFuelLevels = new() {
            { Materials.None, 0 },
            { Materials.Steel, 2 },
            { Materials.Gold, 1 },
            { Materials.Silver, 1 },
            { Materials.Lead, 1 },
            { Materials.Platinum, 3 },
            { Materials.Zinc, 1 },
            { Materials.Stannary, 1 },
            { Materials.Chromium, 3 },
            { Materials.Titanium, 3 },
            { Materials.Nickel, 2 },
            { Materials.Aluminum, 1 },
            { Materials.Uranium, 3 },  // 需要根据实际情况设置
            { Materials.Phosphorus, 1 },  // 需要根据实际情况设置
            { Materials.Iron, 2 },
            { Materials.Copper, 1 },  // 需要根据实际情况设置
            { Materials.Mercury, 1 },  // 需要根据实际情况设置
            { Materials.Germanium, 1 },
            { Materials.FeAlCrAlloy, 2 },
            { Materials.Brass, 1 },
            { Materials.Plastic, 1 },  // 需要根据实际情况设置
            { Materials.Vanadium, 3 },
            { Materials.Cupronickel, 2 },
            //{ Materials.Gun_Steel, 2 },  // 根据实际情况设置
        };

        public static readonly Dictionary<Materials, float> materialMeltingPoints = new() {
            { Materials.None, 0f },          // 无熔点
            { Materials.Steel, 1400f },        // 大致值
            { Materials.Gold, 1064f },
            { Materials.Silver, 961f },
            { Materials.Lead, 327f },
            { Materials.Platinum, 1768f },
            { Materials.Zinc, 419f },
            { Materials.Stannary, 232f },
            { Materials.Chromium, 1907f },
            { Materials.Titanium, 1885f },
            { Materials.Nickel, 1455f },
            { Materials.Aluminum, 660f },
            { Materials.Uranium, 1132f },
            { Materials.Phosphorus, 44f },     // 低熔点非金属
            { Materials.Iron, 1538f },
            { Materials.Copper, 1084f },
            { Materials.Mercury, -39f },       // 液态金属
            { Materials.Germanium, 938f },
            { Materials.FeAlCrAlloy, 1350f },  // 大致范围
            { Materials.Brass, 920f },         // 大致范围
            { Materials.Plastic, 200f },       // 大致范围
            { Materials.Vanadium, 1910f },
            { Materials.Cupronickel, 1200f },  // 大致范围
            //{ Materials.Gun_Steel, 1550 },  // 大致范围
        };

        public static Color GetColor(Materials type)
        {
            switch (type)
            {
                case Materials.Steel:
                case Materials.Phosphorus: return Color.LightGray;
                case Materials.Gold: return new Color(255, 215, 0);
                case Materials.Lead: return new Color(88, 87, 86);
                case Materials.Platinum: return new Color(253, 253, 253);
                case Materials.Chromium: return new Color(58, 57, 56);
                case Materials.Iron: return Color.White;
                case Materials.Copper: return new Color(255, 127, 80);
                case Materials.FeAlCrAlloy: return new Color(200, 200, 200);
                case Materials.Titanium: return Color.DarkGray;
                case Materials.Brass: return new Color(250, 160, 30);
                case Materials.Cupronickel: return new Color(228, 223, 200);
            }
            return new Color(232, 232, 232);
        }

        public static string ToDisplayName(this Materials materials) => LanguageControl.Get("Materials", materials.ToString());

        public static int GetRequiredFuelLevel(Materials material) {
            if (materialFuelLevels.TryGetValue(material, out int fuelLevel)) {
                return materialFuelLevels[material];
            }
            return 0;
        }

        /// <summary>
        /// 通过温度获取黑体颜色，温度单位摄氏度
        /// </summary>
        /// <param name="temperature"></param>
        /// <returns></returns>
        public static Color BlackbodyColor(float temperature) {
            double temp = (temperature + 273.15f) / 100.0;
            // Red
            double red = temp <= 66 ? 255 : Math.Clamp(329.698727446 * Math.Pow(temp - 60, -0.1332047592), 0, 255);
            // Green
            double green = temp <= 66 ? Math.Clamp(99.4708025861 * Math.Log(temp) - 161.1195681661, 0, 255) : Math.Clamp(288.1221695283 * Math.Pow(temp - 60, -0.0755148492), 0, 255);
            // Blue
            double blue = temp >= 66 ? 255 : (temp <= 19 ? 0 : Math.Clamp(138.5177312231 * Math.Log(temp - 10) - 305.0447927307, 0, 255));
            return ((byte)red, (byte)green, (byte)blue);
        }

        public static Color BlendColors(Color color1, Color color2, double ratio) {
            return ((byte)((color1.R * ratio) + (color2.R * (1 - ratio))), (byte)((color1.G * ratio) + (color2.G * (1 - ratio))), (byte)((color1.B * ratio) + (color2.B * (1 - ratio))));
        }

        /// <summary>
        /// 通过材料获取熔融液体颜色
        /// </summary>
        /// <param name="material"></param>
        /// <returns></returns>
        public static Color GetMetalColor(Materials material) {
            var blackBodyColor = BlackbodyColor(materialMeltingPoints[material]);
            double ratio = Math.Clamp((materialMeltingPoints[material] - 1000f) / 6000f, 0, 1); // Adjust ratio based on temperature range
            return BlendColors(GetColor(material), blackBodyColor, ratio);
        }
    }
}